package com.leenmvc.core.web.controller;

import com.leenmvc.core.annotation.ApiDescription;
import com.leenmvc.core.annotation.Note;
import com.leenmvc.core.base.BaseController;
import com.leenmvc.core.security.entity.core.SysApi;
import com.leenmvc.core.security.service.core.SysApiService;
import com.leenmvc.core.utils.DateUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;

/**
 * @Author: JohnCoding
 * @Description:
 * @date: 2020/3/13 9:15
 */
@RestController
@ApiDescription("API接口控制器")
@RequestMapping("system/sysApi")
public class SysApiController extends BaseController<SysApi> {

    @Autowired
    SysApiService sysApiService;

    @GetMapping("exportDocument")
    @Note("导出api数据文档，可选HTML和WORD")
    public void export(@RequestParam String fileType, String fileName, HttpServletResponse response) throws UnsupportedEncodingException {
        response.setCharacterEncoding("UTF-8");
        switch (fileType) { // 根据参数的不同导出不同的文件类型
            case "html":
                response.setHeader("content-type", "application/octet-stream");
                response.setContentType("application/octet-stream");
                response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".html");
                exportHTML(response);
                break;
            case "word":
                response.setContentType("application/msexcle");
                response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1") + ".doc");
                exportWord(response);
                break;
            default:
                exportHTML(response);
                break;
        }
    }

    private void exportWord(HttpServletResponse response) {
        XWPFDocument document = sysApiService.exportWord();
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            document.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void exportHTML(HttpServletResponse response) {
        StringBuilder htmlCode = sysApiService.exportHtml();
        String now = DateUtils.format(new Date(), "yyyyMMddHHmmss");
        OutputStream out = null;
        try {
            response.setContentType("application/x-download");
            response.setHeader("Content-Disposition", "attachment;filename=\""+now+".html\"");
            out = response.getOutputStream();
            out.write(htmlCode.toString().getBytes());
            out.flush();
            out.close();
            response.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
